package com.sw.provider;


import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Product {

//    private static Web3j web3j = Web3j.build(new HttpService());
//    public static void blockRegisterListener(BigInteger blockNum, Receiver receiver) {
//        web3j.catchUpToLatestAndSubscribeToNewBlocksObservable(
//                DefaultBlockParameter.valueOf(blockNum), true).subscribe(block -> {
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            String time = sdf.format(new Date(Long.parseLong(String.valueOf(block.getBlock().getTimestamp())) * 1000L));
//            BlockInfo2 blockInfo = new BlockInfo2();
//            List<TransactionInfo2> txs = new ArrayList<>();
//            List<EthBlock.TransactionResult> transactions = block.getBlock().getTransactions();
//            for (EthBlock.TransactionResult t : transactions){
//                TransactionInfo2 txInfo = new TransactionInfo2();
//                BeanUtils.copyProperties(t,txInfo);
//                txs.add(txInfo);
//                //System.out.println(txInfo);
//            }
//            blockInfo.setNumber(block.getBlock().getNumber())
//                .setHash(block.getBlock().getHash())
//                .setParentHash(block.getBlock().getParentHash())
//                .setMiner(block.getBlock().getMiner())
//                .setDifficulty(block.getBlock().getDifficulty())
//                .setSize(block.getBlock().getSize())
//                .setGasLimit(block.getBlock().getGasLimit())
//                .setGasUsed(block.getBlock().getGasUsed())
//                .setTimestamp(time)
//                .setTxs(txs);
//            receiver.receive(blockInfo);
//        });
//    }

//    public void transactionRegisterListener(BigInteger blockNum, TransactionReceiver receiver) {
//        Subscription subscription = web3j.catchUpToLatestAndSubscribeToNewTransactionsObservable(
//                DefaultBlockParameter.valueOf(blockNum))
//                .subscribe(tx -> {
//                    EthGetTransactionReceipt transactionReceipt = null;
//                    try {
//                        transactionReceipt = web3j.ethGetTransactionReceipt(tx.getHash()).send();
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                    List<Log> logs = transactionReceipt.getResult().getLogs();
//                    //主币交易没有log信息
//                    TransactionInfo transactionInfo = new TransactionInfo();
//                    transactionInfo.setHash(tx.getHash())
//                            .setBlockHash(tx.getBlockHash())
//                            .setBlockNumber(tx.getBlockNumber())
//                            .setFromAddr(tx.getFrom())
//                            .setValue(String.valueOf(tx.getValue()))
//                            .setGasPrice(String.valueOf(tx.getGasPrice()))
//                            .setGasLimit(String.valueOf(tx.getGas()))
//                            .setGasUsed(transactionReceipt.getResult().getGasUsed())  //通过得到交易凭证来获取每笔交易的gas使用量
//                            .setSymBol("ETH交易");
//                    if(logs.isEmpty()){
//                        transactionInfo.setToAddr(tx.getTo());
//                    }else {
//                        String contractAddress = "";
//                        for(Log log : logs){
//                            String address = log.getAddress();
//                            contractAddress = address+" ";
//                        }
//                        transactionInfo.setToAddr(contractAddress);
//                    }
//                    receiver.receive(transactionInfo);
//                });
//
//    }


//    public void USDTTransactionRegisterListener(BigInteger blockNum,USDTTransactionReceiver receiver) {
//        USDTTransactionInfo USDTTransactionInfo = new USDTTransactionInfo();
//        Observable<USDT.TransferEventResponse> eventOb = new USDTapi().transferEventOb(blockNum);
//        eventOb.subscribe(tx2->{
//            EthGetTransactionReceipt transactionReceipt = null;
//            EthTransaction transaction = null;
//            try {
//                transactionReceipt = web3j.ethGetTransactionReceipt(tx2.log.getTransactionHash()).send();
//                transaction = web3j.ethGetTransactionByHash(tx2.log.getTransactionHash()).send();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            USDTTransactionInfo.setToAddr(tx2.to)
//                                .setFromAddr(tx2.from)
//                                .setValue(String.valueOf(tx2.value))
//                                .setHash(tx2.log.getTransactionHash())
//                                .setBlockHash(tx2.log.getBlockHash())
//                                .setBlockNumber(tx2.log.getBlockNumber())
//                                .setSymBol("USDT交易")
//                                .setGasUsed(transactionReceipt.getResult().getGasUsed())
//                                .setGasPrice(String.valueOf(transaction.getResult().getGasPrice()))
//                                .setGasLimit(String.valueOf(transaction.getResult().getGas()));
//            receiver.receive(USDTTransactionInfo);
//        });
//
//    }

}